package com.ruoyi.project.module.ordersDetail.service;

import java.util.*;

import com.ruoyi.common.constant.ErpEnums;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.module.products.domain.Products;
import com.ruoyi.project.module.products.mapper.ProductsMapper;
import com.ruoyi.project.module.products.service.IProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.module.ordersDetail.mapper.OrdersDetailMapper;
import com.ruoyi.project.module.ordersDetail.domain.OrdersDetail;
import com.ruoyi.project.module.ordersDetail.service.IOrdersDetailService;
import com.ruoyi.common.support.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单详情 服务层实现
 *
 * @author lcx
 * @date 2018-08-23
 */
@Service
@Transactional
public class OrdersDetailServiceImpl implements IOrdersDetailService {
    @Autowired
    private OrdersDetailMapper ordersDetailMapper;

    @Autowired
    private IProductsService iProductsService;

    @Autowired
    private ProductsMapper productsMapper;


    /**
     * 查询订单详情信息
     *
     * @param id 订单详情ID
     * @return 订单详情信息
     */
    @Override
    public OrdersDetail selectOrdersDetailById(String id) {
        return ordersDetailMapper.selectOrdersDetailById(id);
    }

    /**
     * 查询订单详情列表
     *
     * @param ordersDetail 订单详情信息
     * @return 订单详情集合
     */
    @Override
    public List<OrdersDetail> selectOrdersDetailList(OrdersDetail ordersDetail) {
        if (StringUtils.isNotEmpty(ordersDetail.getSearchValue())) {
            String[] searchRow = {
                    "O.OrderID",
                    "O.UnitSKUs",
                    "O.UpdateDate"
            };
            return ordersDetailMapper.selectOrdersDetailListAsSearchRow(ordersDetail, searchRow);
        } else {
            return ordersDetailMapper.selectOrdersDetailList(ordersDetail);
        }
    }

    /*计算合计*/
    @Override
    public List<OrdersDetail> selectSumCountOrdersDetailList(OrdersDetail ordersDetail) {
        if (StringUtils.isNotEmpty(ordersDetail.getSearchValue())) {
            String[] searchRow = {
                    "O.OrderID",
                    "O.UnitSKUs",
                    "O.UpdateDate"
            };
            return ordersDetailMapper.selectOrdersDetailListAsSearchRowAsSum(ordersDetail, searchRow);
        } else {
            return ordersDetailMapper.selectOrdersDetailListAsSum(ordersDetail);
        }
    }

    /**
     * 新增订单详情
     *
     * @param ordersDetail 订单详情信息
     * @return 结果
     */
    @Override
    public int insertOrdersDetail(OrdersDetail ordersDetail) {
        return ordersDetailMapper.insertOrdersDetail(ordersDetail);
    }

    @Override
    public int insertOrdersDetail(List<Map<String, Object>> odList) {
        return ordersDetailMapper.insertOrdersDetailByList(odList);
    }

    /**
     * 修改订单详情
     *
     * @param ordersDetail 订单详情信息
     * @return 结果
     */
    @Override
    public int updateOrdersDetail(OrdersDetail ordersDetail) {
        return ordersDetailMapper.updateOrdersDetail(ordersDetail);
    }

    /**
     * 删除订单详情对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteOrdersDetailByIds(String ids) {
        return ordersDetailMapper.deleteOrdersDetailByIds(Convert.toStrArray(ids));
    }


    /**
     * 功能描述:批量修改
     *
     * @param:
     * @return:
     * @auther: cbx
     * @date: 2018/9/14 0014 上午 9:33
     */
    @Override
    public List<Integer> batchUpdateById(String[] ids, String sign) {
        ArrayList<String> skuList = new ArrayList<String>();//发货商品的sku集合
        ArrayList<Integer> numList = new ArrayList<Integer>();//发货商品的数量集合

        ArrayList<OrdersDetail> ordersDetails = new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            OrdersDetail ordersDetail = new OrdersDetail();
            ordersDetail.setId(ids[i]);
            if ("sendout".equals(sign)) {//批量发货
                String updateBy = ShiroUtils.getLoginName();
                //现在的状态改为上一状态
                ordersDetail.setLastStatus("10702");
                //修改后的状态
                ordersDetail.setOrderStatus(ErpEnums.OrderStatus.Shipped_Order.getValue().toString());
                //将发货日期改为当前时间
                ordersDetail.setShippedDate(DateUtils.getTime());
                ordersDetail.setUpdateBy(updateBy);

                //--------------------------修改商品库存信息-------------------------
                OrdersDetail ordersDetail1 = ordersDetailMapper.selectOrdersDetailById(ordersDetail.getId());
                skuList.add(ordersDetail1.getUnitSKUs());
                numList.add(ordersDetail1.getOfItems());


            } else if ("print".equals(sign)) {//批量打印
                ordersDetail.setSendBillPrint(1);
                ordersDetail.setSurfaceBillPrint(1);
                ordersDetails.add(ordersDetail);
            } else if ("delete".equals(sign)) {
                ordersDetail.setIsDel(1);
            }
            ordersDetails.add(ordersDetail);
        }

        ordersDetailMapper.updateBatchByPrint(ordersDetails);//批量修改
        return iProductsService.uploadProductAsMainSkuAndType(skuList, numList, "SoldQuantity");
    }


    /**
     * 功能描述: 查询所有正常订单
     *
     * @param:
     * @return:
     * @auther: cbx
     * @date: 2018/9/15 0015 上午 10:08
     */
    @Override
    public List<OrdersDetail> selectNormalOrder(OrdersDetail ordersDetail) {

        if (StringUtils.isNotEmpty(ordersDetail.getSearchValue())) {
            String[] searchRow = {
                    "O.OrderID",
                    "O.UnitSKUs",
                    "O.UpdateDate"
            };
            return ordersDetailMapper.selectNormalOrderAsSearchRow(ordersDetail, searchRow);
        } else {
            return ordersDetailMapper.selectNormalOrder(ordersDetail);
        }
    }

    /*正常订单合计*/
    @Override
    public List<OrdersDetail> selectSumCountNormalOrder(OrdersDetail ordersDetail) {
        if (StringUtils.isNotEmpty(ordersDetail.getSearchValue())) {
            String[] searchRow = {
                    "O.OrderID",
                    "O.UnitSKUs",
                    "O.UpdateDate"
            };
            return ordersDetailMapper.selectNormalOrderAsSearchRowAsSum(ordersDetail, searchRow);
        } else {
            return ordersDetailMapper.selectNormalOrderAsSum(ordersDetail);
        }
    }

    @Override
    public List<String> updateOrdersDetailBatch(String[] orderIds,String sign) {
        ArrayList<String> skuList = new ArrayList<String>();//发货商品的sku集合
        ArrayList<Integer> numList = new ArrayList<Integer>();//发货商品的数量集合
        List<String> notExistOrders = new ArrayList<String>();//不存在的订单id集合

        ArrayList<OrdersDetail> ordersDetails = new ArrayList<>();
        for (int i = 0; i < orderIds.length; i++) {
            OrdersDetail ordersDetail = new OrdersDetail();

            //--------------------------修改商品库存信息-------------------------
            //根据订单id查询的订单可能为多条
            List<OrdersDetail> ordersDetail1 = ordersDetailMapper.selectOrderByOrderId(orderIds[i].trim(),sign);
            if("".equals(orderIds[i].trim())){
                continue;
            }
            if (ordersDetail1.size()==0){//把数据库不存在的订单id放进来
                notExistOrders.add(orderIds[i].trim());
                continue;
            }else{
                ordersDetail.setOrderID(orderIds[i].trim());
                String updateBy = ShiroUtils.getLoginName();
                //上一状态改为现在的状态
                ordersDetail.setLastStatus("10702");
                //修改后的状态
                ordersDetail.setOrderStatus(ErpEnums.OrderStatus.Shipped_Order.getValue().toString());
                //将发货日期改为当前时间
                ordersDetail.setShippedDate(DateUtils.getTime());
                ordersDetail.setUpdateBy(updateBy);
            }
            for(int j = 0;j<ordersDetail1.size();j++){
                skuList.add(ordersDetail1.get(j).getUnitSKUs());
                numList.add(ordersDetail1.get(j).getOfItems());
                ordersDetails.add(ordersDetail);
            }
        }

        if(ordersDetails.size()!=0){//如果没有不存在的orderId，则发货
            iProductsService.uploadProductAsMainSkuAndType(skuList, numList, "SoldQuantity");//修改库存数量
            ordersDetailMapper.updateOrdersDetailBatch(ordersDetails);//批量修改
        }
        return notExistOrders;
    }

    @Override
    public int batchDeleteRemark(String[] ids, String bz) {
        ArrayList<OrdersDetail> ordersDetails = new ArrayList<>();
        for (int i = 0;i<ids.length;i++){
            OrdersDetail ordersDetail = new OrdersDetail();
            ordersDetail.setBz(bz);
            ordersDetail.setId(ids[i]);
            ordersDetail.setUpdateBy(ShiroUtils.getUser().getUserName());
            ordersDetail.setIsDel(1);
            ordersDetails.add(ordersDetail);
        }
        return ordersDetailMapper.updateOrdersDetailBatchById(ordersDetails);
    }

    @Override
    public List<OrdersDetail> selectOrderDetailsByIds(List<String> ids) {
        return ordersDetailMapper.selectOrderDetailsByIds(ids);
    }

}
